package org.btik.lightweb.client.user;

import jakarta.servlet.http.HttpSession;
import org.btik.light.server.platform.common.api.bean.common.StatusResult;
import org.btik.light.server.platform.common.api.bean.sensitive.annotation.SensitiveBody;
import org.btik.light.server.platform.common.api.bean.sensitive.annotation.SensitiveParams;
import org.btik.light.server.platform.common.api.bean.user.AddUserDto;
import org.btik.light.server.platform.common.api.bean.user.ModifyPasswordDto;
import org.btik.light.server.platform.common.api.bean.user.UpdateUserDto;
import org.btik.light.server.platform.common.api.bean.user.UserVo;
import org.btik.light.server.platform.common.api.service.UserService;
import org.btik.light.tool.DhUtil;
import org.btik.lightweb.client.login.LoginController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

import static org.btik.lightweb.sensitive.SensitiveDtoResolver.DH_PUBLIC_KEY;

/**
 * @author lustre
 * @since 2023/6/12 11:45
 */
@RestController
@RequestMapping("/user")
public class UserManagerController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    private Collection<UserVo> list() {
        return userService.list();
    }

    @PostMapping("/addUser")
    private StatusResult<Void> addUser(@SensitiveBody AddUserDto addUserDto) {
        return userService.addUser(addUserDto);
    }

    @PostMapping("/updateUser")
    private StatusResult<Void> updateUser(@SensitiveBody UpdateUserDto updateUserDto, HttpSession httpSession) {
        UserVo loginUser = (UserVo) httpSession.getAttribute(LoginController.CURRENT_USER);
        return userService.updateUser(updateUserDto, loginUser);
    }

    @GetMapping("/unlockUser")
    private StatusResult<Void> unlockUser(@SensitiveParams("account") String account) {
        return userService.unlockUser(account);
    }

    @GetMapping("/changeUserStatus")
    private StatusResult<Void> changeUserStatus(@SensitiveParams("account") String account, boolean status) {
        return userService.changeUserStatus(account, status);
    }

    @GetMapping("/deleteUser")
    private StatusResult<Void> deleteUser(@SensitiveParams("account") String account) {
        return userService.deleteUser(account);
    }

    @GetMapping("/resetPassword")
    private StatusResult<String> resetPassword(String account, @RequestHeader(DH_PUBLIC_KEY) String publicKey, HttpSession httpSession) {
        DhUtil.DhKeyPair dhKeyPair = (DhUtil.DhKeyPair) httpSession.getAttribute(LoginController.DH_KEY_PAIR);
        return userService.resetPassword(account, publicKey, dhKeyPair);
    }

    @PostMapping("/modifyPassword")
    private StatusResult<Void> modifyPassword(@SensitiveBody ModifyPasswordDto modifyPasswordDto, HttpSession httpSession) {
        UserVo loginUser = (UserVo) httpSession.getAttribute(LoginController.CURRENT_USER);
        return userService.modifyPassword(modifyPasswordDto, loginUser.getAccount());
    }

}
